/*
 * @Author: hanxin 
 * @Date: 2021-08-11 21:08:19 
 * @Last Modified by: hanxin
 * @Last Modified time: 2021-08-15 23:30:59
 * 客户端界面
 */
package view;

import service.fileService;
import service.messageService;
import service.userService;

import org.fusesource.jansi.AnsiConsole;


public class cliView {

    private userService service = new userService();
    private messageService messageService = new messageService();
    private fileService fileService = new fileService();
    private boolean loop = true;

    public static void main(String[] args) {
        AnsiConsole.systemInstall();
        new cliView().mainMenu();

    }

    public void mainMenu() {
        String key = " ";
        System.out.println(
                "\n\33[32;1m██╗    ██╗███████╗██╗      ██████╗ ██████╗ ███╗   ██╗███╗   ███╗███████╗    ████████╗ ██████╗     ███╗   ███╗██╗   ██╗ ██████╗███████╗"
                        + "\n██║    ██║██╔════╝██║     ██╔════╝██╔═══██╗████╗  ██║████╗ ████║██╔════╝    ╚══██╔══╝██╔═══██╗    ████╗ ████║██║   ██║██╔════╝██╔════╝"
                        + "\n██║ █╗ ██║█████╗  ██║     ██║     ██║   ██║██╔██╗ ██║██╔████╔██║█████╗         ██║   ██║   ██║    ██╔████╔██║██║   ██║██║     ███████╗"
                        + "\n██║███╗██║██╔══╝  ██║     ██║     ██║   ██║██║╚██╗██║██║╚██╔╝██║██╔══╝         ██║   ██║   ██║    ██║╚██╔╝██║██║   ██║██║     ╚════██║"
                        + "\n╚███╔███╔╝███████╗███████╗╚██████╗╚██████╔╝██║ ╚████║██║ ╚═╝ ██║███████╗       ██║   ╚██████╔╝    ██║ ╚═╝ ██║╚██████╔╝╚██████╗███████║"
                        + "\n╚══╝╚══╝ ╚══════╝╚══════╝ ╚═════╝ ╚═════╝ ╚═╝  ╚═══╝╚═╝     ╚═╝╚══════╝       ╚═╝    ╚═════╝     ╚═╝     ╚═╝ ╚═════╝  ╚═════╝╚══════╝");

        System.out.format("\n \33[33;1mHere you can transfer files or messages privately."
                + "\nWelcome to register for MUCS" + "\n--help command will prompt operation instructions\33[0m");

        while (loop) {
            System.out.println("\n \033[32;2m ");
            System.out.println("1: " + "login");
            System.out.println("2: " + "register");
            System.out.println("9: " + "exit");

            System.out.print("\nplease_input...");

            key = utility.readString(1);
            switch (key) {
                case "1":
                    System.out.println("\nenter 'q' back");
                    System.out.println("username?...");
                    String uid = utility.readString(50);
                    if (uid.equals("q") || uid.equals("Q")) {
                        break;
                    }
                    System.out.println("passwd?...");
                    String pwd = utility.readString(50);
                    if (pwd.equals("q") || pwd.equals("Q")) {
                        break;
                    }
                    if (service.checkUserLogin(uid, pwd)) { // 构建message对象发送到服务端验证

                        System.out.println("login_success..\n");

                        boolean loop = true;
                        System.out.println("\n-!@$23--13445____layer 2____!@$23--13445-");

                        System.out.println("if you want to View function please input --help");

                        // 登录成功进入二级菜单
                        while (loop) {

                            System.out.println("\ncurrent :" + uid);// 显示当前用户

                            key = utility.readString(10);
                            switch (key) {
                                case "1":

                                    service.getOnlineLIst();
                                    break;
                                case "2":
                                    System.out.println("\nIf you want to exit the chat, enter q");
                                    System.out.print("To_All:");
                                    String Masscontent = utility.readString(100);
                                    if (Masscontent.equals("q") && Masscontent.equals("Q")) {
                                        break;
                                    }
                                    messageService.sendMassMassage(Masscontent, uid);

                                    break;
                                case "3":
                                    System.out.println("\nIf you want to exit the chat, enter q");
                                    System.out.print("To whom:");
                                    String getter = utility.readString(10);
                                    if (getter.equals(uid)) {
                                        System.out.println("wrong_sender");
                                        break;
                                    }
                                    if (getter.equals("q") || getter.equals("Q")) {
                                        break;
                                    }

                                    Boolean chatloop = true;
                                    while (chatloop) {
                                        System.out.print("please_input_content:");
                                        String content = utility.readString(100);

                                        if (content.equals("q") || content.equals("Q")) {
                                            break;
                                        }

                                        messageService.sendMessage(content, getter, uid);
                                    }

                                    break;
                                case "4":
                                    System.out.println("\nIf you want to exit that, enter q");
                                    System.out.print("To whom:");
                                    String filegetter = utility.readString(10);
                                    if (filegetter.equals(uid)) {
                                        System.out.println("wrong_sender");
                                        break;
                                    }
                                    if (filegetter.equals("q") || filegetter.equals("Q")) {
                                        break;
                                    }
                                    System.out.println("Example:***/***/***.jpg");
                                    System.out.print("Local_File_Path:");
                                    String src = utility.readString(50);
                                    if (src.equals("q") || src.equals("Q")) {
                                        break;
                                    }

                                    if (!src.contains("/")) {// 用户输入不包含路径退出
                                        System.out.println("wrong_path");
                                        break;
                                    }
                                    fileService.sendfile(uid, filegetter, src);
                                    break;
                                    // System.out.println("Send_file");
                                   
                                case "9":
                                    loop = false;
                                    service.logOut();
                                    break;
                                case "--help":
                                    help();
                                    break;
                                case "--HELP":
                                    help();
                                    break;
                                default:
                                    System.out.println("error_command");
                                    break;
                            }
                        }
                    } else {
                        System.out.println("login_fail...");
                    }
                    break;

                case "2":
                    System.out.println("\nenter 'q' back");
                    System.out.print("input_your_username:");
                    String user = utility.readString(10);
                    if (user.equals("q") || user.equals("Q")) {
                        break;
                    }
                    System.out.print("input_your_passwd:");
                    String passwd = utility.readString(10);
                    if (passwd.equals("q") || passwd.equals("Q")) {
                        break;
                    }
                    service.register(user, passwd);
                    break;
                case "9":
                    loop = false;
                    // 关闭进程->线程
                    System.exit(0);
                    System.out.println("close...");
                default:
                    break;
            }
        }
    }

    public void help() {
        System.out.println("\n___                         _    _            "
                + "\n .' ..]                       / |_ (_)               "
                + "\n_| |_  __   _  _ .--.   .---.`| |-'__   .--.  _ .--.   "
                + "\n'-| |-'[  | | |[ `.-. | / /'`\\]| | [  |/ .'`\\ [ `.-. |"
                + "\n| |   | \\_/ |,| | | | | \\__. | |, | || \\__. || | | | "
                + "\n[___]  '.__.'_[___||__]'.___.'\\__/[___]'.__.'[___||__]");
        System.out.println("\n1: " + "online_list");
        System.out.println("2: " + "Mass_mailing");
        System.out.println("3: " + "Private_chat");
        System.out.println("4: " + "Send_file");
        System.out.println("9: " + "exit_login");
    }
}
